<?php

class UserMemberModel
{
    private $userMemberKeyPrefix    =   'UM/';
    private $userVipOrderKeyPrefix  =   'VO/';
    private $slaveKey           =   '/slave';

    public function getMemberInfo($info, $ext = false)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'UMM001';
            return  FALSE;
        }

        $key    =   $this->userMemberKeyPrefix. $info['uid'].$this->slaveKey;
        $result =   Storage::getData($key);
        //TODO 扩展字段处理
        if($result['extra'] && $ext){
	        $extra = json_decode($result['extra'], TRUE);
	        if(empty($extra)){
	        	$result['todayExp']  =   0;
		        $result['privateDiary'] = 0;
		        $result['deleteDiary'] = 0;
		        $result['deleteDiaryByadmin'] = 0;
		        $result['deleteCommentByadmin'] = 0;
		        $result['deleteTopic'] = 0; // 话题删除
		        $result['deleteTopicByadmin'] = 0; // 话题删除
		        $result['forbidUser'] = 0;// 被封禁减少50%经验
		        $result['gagUser1'] = 0; // 第1次禁言
		        $result['gagUser2'] = 0; // 第2次禁言
		        $result['gagUser3'] = 0; // 第3次禁言
		        return $result;
	        }
	        $result['extra'] = $extra;
	        $result['todayExp']  =   $extra['todayExp'];
	        $result['privateDiary'] = $extra['privateDiary'];
	        $result['deleteDiary'] = $extra['deleteDiary'];
	        $result['deleteDiaryByadmin'] = $extra['deleteDiaryByadmin'];
	        $result['deleteCommentByadmin'] = $extra['deleteCommentByadmin'];
	        $result['deleteTopic'] = $extra['deleteTopic']; // 话题删除
	        $result['deleteTopicByadmin'] = $extra['deleteTopicByadmin']; // 话题删除
	        $result['forbidUser'] = $extra['forbidUser'];// 被封禁减少50%经验
	        $result['gagUser1'] = $extra['gagUser1']; // 第1次禁言
	        $result['gagUser2'] = $extra['gagUser2']; // 第2次禁言
	        $result['gagUser3'] = $extra['gagUser3']; // 第3次禁言
        }       
        unset($result['extra']);
        return  $result;
    }/*}}}*/
    public function initUserMember($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'UMM001';
            return  FALSE;
        }

        $data   =   array('uid' => $info['uid']);
        $key    =   $this->userMemberKeyPrefix. $info['uid'];
        $result =   Storage::addData($key, $data);

        return  $result;
    }/*}}}*/

    public function setMemberInfo($info, $setExtra)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'UMM001';
            return  FALSE;
        }

        $data   =   array();
        isset($info['traffic'])         &&  $data['traffic']        =   $info['traffic'];
        isset($info['allTraffic'])      &&  $data['allTraffic']     =   $info['allTraffic'];
        isset($info['status'])          &&  $data['status']         =   $info['status'];
        isset($info['level'])           &&  $data['level']          =   $info['level'];
        isset($info['exp'])             &&  $data['exp']            =   $info['exp'];
        isset($info['profitDay'])       &&  $data['profitDay']      =   $info['profitDay'];
        isset($info['diaryTimes'])      &&  $data['diaryTimes']     =   $info['diaryTimes'];
        isset($info['commentTimes'])    &&  $data['commentTimes']   =   $info['commentTimes'];
        isset($info['likeTimes'])       &&  $data['likeTimes']      =   $info['likeTimes'];
        isset($info['likedTimes'])      &&  $data['likedTimes']     =   $info['likedTimes'];
        isset($info['diaryTagTimes'])   &&  $data['diaryTagTimes']  =   $info['diaryTagTimes'];
        // group
        isset($info['addTopicTimes'])   &&  $data['addTopicTimes']  =   $info['addTopicTimes'];
        isset($info['postTopicCommentTimes'])      &&  $data['postTopicCommentTimes']     =   $info['postTopicCommentTimes'];
        // 转发以及被转发
        isset($info['repostTimes'])     &&  $data['repostTimes']    =   $info['repostTimes'];
        isset($info['repostedTimes'])   &&  $data['repostedTimes']  =   $info['repostedTimes'];

        if (empty($data))   return  TRUE;
        if (isset($data['traffic']) &&  $data['traffic'] <  0)  $data['traffic']    =   0;

	// TODO 添加扩展字段
	if($setExtra){
		$extra= array();
		$extra['todayExp'] = $info['todayExp'];
		$extra['privateDiary'] = $info['privateDiary'];
		$extra['deleteDiary'] = $info['deleteDiary'];
		$extra['deleteDiaryByadmin'] = $info['deleteDiaryByadmin'];
		$extra['deleteCommentByadmin'] = $info['deleteCommentByadmin'];
		$extra['deleteTopic'] = $info['deleteTopic']; // 话题删除
		$extra['deleteTopicByadmin'] = $info['deleteTopicByadmin']; // 话题删除
		$extra['forbidUser'] = $info['forbidUser'];// 被封禁减少50%经验
		$extra['gagUser1'] = $info['gagUser1']; // 第1次禁言
		$extra['gagUser2'] = $info['gagUser2']; // 第2次禁言
		$extra['gagUser3'] = $info['gagUser3']; // 第3次禁言
	        
		$data['extra'] = json_encode($extra);
	}
        
        $key    =   $this->userMemberKeyPrefix. $info['uid'];
        $result =   Storage::setData($key, $data);

        return  $result;
    }/*}}}*/
    
    public function changeExpLevel($info)/*{{{*/
    {
    	if (empty($info['uid']))
    	{
    		AC::$errorNo    =   'UMM001';
    		return  FALSE;
    	}
    	
    	$data   =   array();
    	isset($info['level'])           &&  $data['level']          =   $info['level'];
    	isset($info['exp'])             &&  $data['exp']            =   $info['exp'];
    
    	if (empty($data))   return  TRUE;
    	
    	$key    =   $this->userMemberKeyPrefix. $info['uid'];
    	$result =   Storage::setData($key, $data);
    
    	return  $result;
    }/*}}}*/

    public function updateUserTraffic ($uid, $traffic) {
        if (empty($uid) || empty($traffic)) return false;

        $index      =   UK::getHashId($uid, 8);
        $sql        =   "Update xxtuser.usermember_".$index." SET `traffic` = `traffic` + ".$traffic.", `buyTraffic` = `buyTraffic` + ".$traffic." WHERE `uid` = ".$uid." LIMIT 1";

        $keyClient  =   $this->userMemberKeyPrefix. $index;
        $result     =   Storage::execDbSentenceByClient($keyClient, $sql);

        return $result;
    }

    /**
     * 更新会员流量
     * @param $uid
     * @param $traffic
     * @return bool|int|resource
     */
    public function updateUserVipTraffic ($uid, $traffic) {
        if (empty($uid) || empty($traffic)) return false;

        $index      =   UK::getHashId($uid, 8);
        $sql        =   "Update xxtuser.usermember_".$index." SET `traffic` = `traffic` + ".$traffic.", `vipTraffic` = ".$traffic." WHERE `uid` = ".$uid." AND `vipTraffic`!=".$traffic."  LIMIT 1";

        $keyClient  =   $this->userMemberKeyPrefix. $index;
        $result     =   Storage::execDbSentenceByClient($keyClient, $sql);

        return $result;
    }

    /**
     * 重置会员流量
     * @param $uid
     * @param $traffic
     * @return bool|int|resource
     */
    public function resetUserVipTraffic ($uid) {
        if (empty($uid)) return false;

        $memberInfo = $this->getMemberInfo(['uid'=>$uid]);
        if (empty($memberInfo))return;
        $traffic = $memberInfo['level']<5 ? ExpLevel::$levelMapFlow[5] : ExpLevel::$levelMapFlow[ $memberInfo['level'] ];
        $traffic = $traffic * 1024;
        $index      =   UK::getHashId($uid, 8);
        $sql        =   "Update xxtuser.usermember_".$index." SET `allTraffic`=".$traffic.", `vipTraffic`=0 WHERE `uid` = ".$uid." LIMIT 1";

        if ($memberInfo['traffic']>$traffic){
            $sql        =   "Update xxtuser.usermember_".$index." SET `traffic` = ".$traffic.", `allTraffic`=".$traffic.", `vipTraffic`=0 WHERE `uid` = ".$uid." LIMIT 1";
        }

        $keyClient  =   $this->userMemberKeyPrefix. $index;
        $result     =   Storage::execDbSentenceByClient($keyClient, $sql);

        return $result;
    }

    /**
     * 新增ios单月会员续订订单
     * @param $data
     * @return bool|int|resource
     */
    public function addUserVipOrder ($data) {
        if (empty($data['uid'])) return false;

        $keyClient  =   $this->userVipOrderKeyPrefix. 0;
        $result     =   Storage::addData($keyClient, $data);
        return $result;
    }
}

